package com.ruoyi.basebill.controller;

import com.ruoyi.basebill.domain.*;
import com.ruoyi.basebill.service.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;


@Validated
@RestController
@RequestMapping("/api/jszx")
public class JszxApiController {

    //收货确认服务层
    @Autowired
    public IOmsReceiptbillService omsReceiptbillService;

    //发货确认服务层
    @Autowired
    public IOmsDeliverbillService omsDeliverbillService;

    //销售退货单服务层
    @Autowired
    public IOmsSalereturnbillService salereturnbillService;

    //调拨单服务层
    @Autowired
    public IOmsTransferbillService transferbillService;

    @Autowired
    private IOmsSaleorderbillService omsSaleorderbillService;

    @Autowired
    private  IJszxApiService jszxApiService;

    @ApiOperation("OMS收货确认")
    @Log(title = "OMS收货确认",businessType = BusinessType.OMS_API)
    @PostMapping("/ReceiptBill")
    public AjaxResult importReceiptBill(@RequestBody @Valid OmsReceiptbill receiptbill){
        AjaxResult ajax = AjaxResult.success();
        AjaxResult error = AjaxResult.error();
        String code = receiptbill.getCode();
        if(StringUtils.isEmpty(code)){
            error.put("msg","单据编码：code为空");
            error.put("state","0");
            return error;
        }
        OmsReceiptbill omsReceiptbill = new OmsReceiptbill();
        omsReceiptbill.setCode(code);
        List<OmsReceiptbill> omsReceiptbills = omsReceiptbillService.selectOmsReceiptbillList(omsReceiptbill);
        if(omsReceiptbills.size()>0){
            error.put("msg","该单据"+code+"已经同步");
            error.put("state","0");
            return error;
        }
        StringBuffer buffer = jszxApiService.checkBill(receiptbill);
        if(!"Over".equals(buffer.toString())){
            error.put("msg",buffer.toString());
            error.put("state","0");
            return error;
        }
        int i = omsReceiptbillService.insertOmsReceiptbill(receiptbill);
        if (i == 1){
            ajax.put("msg","保存成功");
            ajax.put("state","1");
        }else {
            ajax.put("msg","保存失败");
            ajax.put("state","0");
        }
        return ajax;
    }

    @ApiOperation("OMS发货确认")
    @Log(title = "OMS发货确认",businessType = BusinessType.OMS_API)
    @PostMapping("/Deliverbill")
    public AjaxResult importDeliverbill(@RequestBody @Valid OmsDeliverbill deliverbill){
        AjaxResult ajax = AjaxResult.success();

        AjaxResult error = AjaxResult.error();
        String code = deliverbill.getCode();
        if(StringUtils.isEmpty(code)){
            error.put("msg","单据编码：code为空");
            error.put("state","0");
            return error;
        }
        OmsDeliverbill omsDeliverbill = new OmsDeliverbill();
        omsDeliverbill.setCode(code);
        List<OmsDeliverbill> omsDeliverbills = omsDeliverbillService.selectOmsDeliverbillList(omsDeliverbill);
        if(omsDeliverbills.size()>0){
            error.put("msg","该单据"+code+"已经同步");
            error.put("state","0");
            return error;
        }
        //校验
        StringBuffer buffer = jszxApiService.checkBill(deliverbill);
        if(!"Over".equals(buffer.toString())){
            error.put("msg",buffer.toString());
            error.put("state","0");
            return error;
        }


        int i = omsDeliverbillService.insertOmsDeliverbill(deliverbill);
        if (i == 1){
            ajax.put("msg","保存成功");
            ajax.put("state","1");
        }else {
            ajax.put("msg","保存失败");
            ajax.put("state","0");
        }
        return ajax;
    }

    @ApiOperation("OMS销售退货单")
    @Log(title = "OMS销售退货单",businessType = BusinessType.OMS_API)
    @PostMapping("/SaleReturnBill")
    public AjaxResult importSaleReturnBill(@RequestBody @Valid OmsSalereturnbill salereturnbill){
        AjaxResult ajax = AjaxResult.success();
        AjaxResult error = AjaxResult.error();
        String code = salereturnbill.getCode();
        if(StringUtils.isEmpty(code)){
            error.put("msg","单据编码：code为空");
            error.put("state","0");
            return error;
        }

        OmsSalereturnbill omsSalereturnbill = new OmsSalereturnbill();
        omsSalereturnbill.setCode(code);
        List<OmsSalereturnbill> omsSalereturnbills = salereturnbillService.selectOmsSalereturnbillList(omsSalereturnbill);
        if(omsSalereturnbills.size()>0){
            error.put("msg","该单据"+code+"已经同步");
            error.put("state","0");
            return error;
        }
        //校验
        StringBuffer buffer = jszxApiService.checkBill(salereturnbill);
        if(!"Over".equals(buffer.toString())){
            error.put("msg",buffer.toString());
            error.put("state","0");
            return error;
        }
        String srcsys = salereturnbill.getSrcsys();
        if("zc".equals(srcsys)){
            StringBuffer buffer1 = jszxApiService.calculateReturnbill(salereturnbill);
            if(buffer1.length()>0){
                error.put("msg",buffer1.toString());
                error.put("state","0");
                return error;
            }
        }

        int i = salereturnbillService.insertOmsSalereturnbill(salereturnbill);
        if (i == 1){
            ajax.put("msg","保存成功");
            ajax.put("state","1");
        }else {
            ajax.put("msg","保存失败");
            ajax.put("state","0");
        }
        return ajax;
    }
    @ApiOperation("OMS调拨单")
    @Log(title = "OMS调拨单",businessType = BusinessType.OMS_API)
    @PostMapping("/TransferBill")
    public AjaxResult importTransferBill(@RequestBody @Valid OmsTransferbill transferbill){
        AjaxResult ajax = AjaxResult.success();
        AjaxResult error = AjaxResult.error();
        String code = transferbill.getCode();
        if(StringUtils.isEmpty(code)){
            error.put("msg","单据编码：code为空");
            error.put("state","0");
            return error;
        }
        OmsTransferbill omsTransferbill = new OmsTransferbill();
        omsTransferbill.setCode(code);
        List<OmsTransferbill> omsTransferbills = transferbillService.selectOmsTransferbillList(omsTransferbill);
        if(omsTransferbills.size()>0){
            error.put("msg","该单据"+code+"已经同步");
            error.put("state","0");
            return error;
        }
        //校验
        StringBuffer buffer = jszxApiService.checkBill(transferbill);
        if(!"Over".equals(buffer.toString())){
            error.put("msg",buffer.toString());
            error.put("state","0");
            return error;
        }

        String getsrcsys = transferbill.getsrcsys();
        if("pos".equals(getsrcsys)){ //早餐另外逻辑
            jszxApiService.getTransferBill(transferbill);
        }else if("wdt".equals(getsrcsys)){//旺店通配置表
            jszxApiService.getTransferBillByWdt(transferbill);
        }

        int i = transferbillService.insertOmsTransferbill(transferbill);
        if (i == 1){
            ajax.put("msg","保存成功");
            ajax.put("state","1");
        }else {
            ajax.put("msg","保存失败");
            ajax.put("state","0");
        }
        return ajax;
    }

    @ApiOperation("OMS销售订单")
    @Log(title = "OMS销售订单",businessType = BusinessType.OMS_API)
    @PostMapping("/SaleOrderBill")
    public AjaxResult importSaleOrderBill(@RequestBody OmsSaleorderbill omsSaleorderbill){
        AjaxResult ajax = AjaxResult.success();
        int i = omsSaleorderbillService.insertOmsSaleorderbill(omsSaleorderbill);
        if (i == 1){
            ajax.put("msg","保存成功");
            ajax.put("state","1");
        }else {
            ajax.put("msg","保存失败");
            ajax.put("state","0");
        }
        return ajax;
    }

}






